package com.example.util;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class MailUtil{

	@Autowired	
	private JavaMailSender javaMailSender;
    
    
    @Value("${mail.fromMail.addr}")
    private String from;
    
    //@Slf4j
    private final Logger logger= LoggerFactory.getLogger(this.getClass());
    
    private String fileName;
    
	public void sendSimpleMail(String to,String subject,String content){       //发送简单的邮件
  	  SimpleMailMessage simpleMailMessage =new SimpleMailMessage();
  	  simpleMailMessage.setFrom(from);
  	  simpleMailMessage.setTo(to);
  	  simpleMailMessage.setSubject(subject);
  	  simpleMailMessage.setText(content);
  	    
  	  try{
  		  javaMailSender.send(simpleMailMessage);
  		  logger.info("简单邮件已发送"); 
  	  }catch(Exception e){
  		  logger.error("发送邮件异常",e);
  	  }
  	  
    }
    
    public void sendHtmlMail(String to,String subject,String content){           //发送带有html格式的邮件
  	  MimeMessage mimeMessage= javaMailSender.createMimeMessage();
  	  
  	  try{
  		  MimeMessageHelper helper= new MimeMessageHelper(mimeMessage,true);
  		  
  		  
  		  helper.setFrom(from);
  		  helper.setTo(to);
  		  helper.setSubject(subject);
  		  helper.setText(content,true);
  		  javaMailSender.send(mimeMessage);
  		  logger.info 

("html格式的邮件发送成功!");
  	  }catch(Exception e){
  		  logger.error("html格式的邮件发送失败!",e);  		  
  	  }
  	  
    }
    
    public void sendAttachementsMail(String to,String subject,String content,String filepath){    //发送带有附件的邮件
  	  MimeMessage mimeMessage= javaMailSender.createMimeMessage();
  	  
  	  try{
  		  MimeMessageHelper helper= new MimeMessageHelper(mimeMessage,true);
  		  helper.setFrom(from);
  		  helper.setTo(to);
  		  helper.setSubject(subject);
  		  helper.setText(content,true);
  		  
  		  FileSystemResource fileSystemResource= new FileSystemResource(new File(filepath)); 
  		  fileName=filepath.substring(filepath.lastIndexOf(File.separator));
  		  helper.addAttachment(fileName,fileSystemResource);
  		  
  		  javaMailSender.send(mimeMessage);
  		  logger.info 

("带有附件的邮件发送成功!");
  	  }catch(Exception e){
  		  logger.error("带有附件的邮件发送失败!",e);
  	  }
    }
    
    public void sendInLineResource(String to,String subject,String content,String filepath,String rscId){     //发送带有静态资源的邮件
  	  MimeMessage mimeMessage= javaMailSender.createMimeMessage();
  	  
  	  try{
  		  MimeMessageHelper helper= new MimeMessageHelper(mimeMessage,true);
  		  helper.setFrom(from);
  		  helper.setTo(to);
  		  helper.setSubject(subject);
  		  helper.setText(content,true);
  		  
  		  FileSystemResource fileSystemResource= new FileSystemResource(new File(filepath));
  		  helper.addInline(rscId,fileSystemResource);
  		  
  		  javaMailSender.send(mimeMessage);
  		  logger.info 

("发送一个静态资源的文件的附件");
  	  }catch(Exception e){
  		  logger.error("发送一个静态资源的文件的附件失败",e);
  	  }
    }
}
